package com.yunzai.secure.point;

import com.yunzai.generic.domain.JSONResult;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class YunzaiAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private static final Logger log = LogManager.getLogger(YunzaiAuthenticationEntryPoint.class);

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.error("未登录！或登陆状态已过期！", authException);
        String string = JSONResult.error(401, "未登录！或登陆状态已过期！").toString();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(string);
    }
}
